package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper flavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void saveWithFavour(DishDTO dishDTO) {
        /*
        * 插入菜品数据
        * */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        long id = dish.getId();
        /*
        * 插入口味数据
        * */
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()!=0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            flavorMapper.save(flavors);
        }
    }
    /*
    * 分页查询
    * */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        Page<DishVO> dishes = (Page<DishVO>) dishMapper.pageQuery(dish);
        return new PageResult(dishes.getTotal(),dishes.getResult());
    }

    @Override
    @Transactional
    public void delete(long[] ids) {
        dishMapper.delete(ids);
        dishMapper.deleteFlavor(ids);
    }

    @Override
    public boolean findStatus(long[] ids) {
        int[] statuss = dishMapper.findStatus(ids);
        boolean flag = true;
        for (Integer status : statuss) {
            if(status == 1){
                flag = false;  //如果是false说明有在起售的菜品
            }
        }
        return flag;
    }

    @Override
    public List<SetmealDish> findConnection(long[] ids) {
        List<SetmealDish> list= setmealDishMapper.findConnection(ids);
        return list;
    }

    @Override
    public DishVO select(Long id) {
        DishVO dishVO = dishMapper.select(id);
        List<DishFlavor> flavors = flavorMapper.select(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDTO.getId());
        }
        flavorMapper.delete(dishDTO.getId());
        if(flavors!=null && flavors.size()!=0){
            flavorMapper.save(flavors);
        }
    }

    @Override
    public void modifyStatus(Integer status, long id) {
        dishMapper.modifyStatus(status,id);
    }

    @Override
    public List<Dish> findByCategoryId(Integer categoryId) {
        List<Dish> list = dishMapper.findByCategoryId(categoryId);
        return list;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        List<DishVO> dishList = dishMapper.pageQuery(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishList) {
            List<DishFlavor> flavors = dishFlavorMapper.select(d.getId());
            d.setFlavors(flavors);
            dishVOList.add(d);
        }

        return dishVOList;
    }

}
